package best.driver.android.Activity.Finance;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.Activity.Finance.Adapter.TradeInfoAdapter;
import best.driver.android.Beans.TradeInfo;
import best.driver.android.Beans.TradeItemInfo;
import best.driver.android.Event.WithdrawEvent;
import best.driver.android.R;
import best.driver.android.RequestFactory;
import best.driver.android.Utils.Util;
import best.driver.android.View.BasePullToRefreshListView;
import best.driver.android.common.app.AppProxyFactory;
import best.driver.android.common.network.ApiObjectRequest;
import best.driver.android.common.network.ApiRequest;
import best.driver.android.common.network.BestError;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

import java.util.ArrayList;
import java.util.List;

public class IncomeActivity extends BaseActivity implements BasePullToRefreshListView.OnPullDownListener{

    @InjectView(R.id.tv_valid_money)
    TextView mTvValidMoney;

    @InjectView(R.id.tv_remain_money)
    TextView mTvRemainMoney;

    @InjectView(R.id.btn_withdraw)
    TextView mBtnWithdraw;

    @InjectView(R.id.ll_no_data)
    LinearLayout mLlNoData;

    @InjectView(R.id.plv_trade_record)
    BasePullToRefreshListView mPlvTradeRecord;

    double mValidMoney = 0;

    double mTotalMoney = 0;

    long mTime;

    private List<TradeItemInfo> mItems = new ArrayList<TradeItemInfo>();

    private int mPage = 1;

    private final int PAGE_SIZE = 10;

    private TradeInfoAdapter mAdapter;

    @OnClick(R.id.btn_withdraw)
    protected void withdraw() {
        Intent intent = new Intent(IncomeActivity.this, WithdrawActivity.class);
        intent.putExtra("money", mValidMoney);
        intent.putExtra("time", mTime);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_income);
        ButterKnife.inject(this, view);
        setLeftIvBak(R.drawable.icon_navibar_back);
        EventBus.getDefault().register(this);
        setTopTopic("我的账户");
        view.setVisibility(View.GONE);
        setBtnText(null, "收入统计");
        mAdapter = new TradeInfoAdapter(this);
        mPlvTradeRecord.setAdapter(mAdapter);
        mPlvTradeRecord.setRefreshEnabled(true);
        mPlvTradeRecord.setOnPullDownListener(this);
        mPlvTradeRecord.showAutoLoadMore();
        mLlNoData.setVisibility(View.GONE);
        mBtnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(IncomeActivity.this, IncomeStatisticsActivity.class);
                startActivity(intent);
            }
        });
        getTradesRequest(mPage, PAGE_SIZE, true, false);
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getTradesRequest(mPage, PAGE_SIZE, false, true);
    }

    @Override
    public void onMore() {
        getTradesRequest(mPage, PAGE_SIZE, false, false);
    }

    public void updateView() {
        view.setVisibility(View.VISIBLE);
        if(mValidMoney > 0.0099) {
            mBtnWithdraw.setEnabled(true);
            mBtnWithdraw.setBackgroundResource(R.drawable.login_btn_border_red);
        } else {
            mBtnWithdraw.setEnabled(false);
            mBtnWithdraw.setBackgroundResource(R.drawable.login_btn_border_gray);
        }
        mTvRemainMoney.setText(Util.getOnePointNumber(mTotalMoney));
        mTvValidMoney.setText(Util.getOnePointNumber(mValidMoney));
    }



    @Override
    protected void onBack() {
        this.finish();
    }

    private void getTradesRequest(int page, int pageSize, boolean isFirst, final boolean isRefresh) {
        if(isFirst) {
            showWaiting();
        }
        final ApiObjectRequest<TradeInfo> request = RequestFactory.createGetTradesRequest(page, pageSize);
        request.setListener(new ApiRequest.ApiRequestListener<TradeInfo>() {
            @Override
            public void onRequestSuccess(TradeInfo response) {
                hideWaiting();
                mTotalMoney = response.availableBalance;
                mValidMoney = response.currentAvailableBalance;
                mTime = response.toAccountDate;
                mPlvTradeRecord.onRefreshComplete();
                mPlvTradeRecord.onLoadMoreComplete();
                if(!isRefresh) {
                    mPage ++;
                    mItems.addAll(response.transactions);
                } else {
                    mItems.clear();
                    mItems.addAll(response.transactions);
                }
                mAdapter.setData(mItems);
                if(response.lastPageFlag) {
                    mPlvTradeRecord.hideAutoLoadMore();
                } else {
                    mPlvTradeRecord.showAutoLoadMore();
                }

                updateView();
                if(mItems.size() == 0) {
                    mLlNoData.setVisibility(View.VISIBLE);
                    mPlvTradeRecord.setVisibility(View.GONE);
                }

            }

            @Override
            public void onRequestError(BestError error) {
                hideWaiting();
                mPlvTradeRecord.onRefreshComplete();
                mPlvTradeRecord.onLoadMoreComplete();
            }
        });
        AppProxyFactory.getProxy().getNetworkManager().enqueueRequest(request);
    }

    public void onEventMainThread(WithdrawEvent event) {
        mValidMoney = mValidMoney - event.withdrawMoney;
        mTotalMoney = mTotalMoney - event.withdrawMoney;
        updateView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
